import Logger from '../base/logger';
import irtc from './irtc.min.js';
//const irtc = require('./irtc.min.js');

class IRtcMiniClient {
    /**
     * 构造函数，初始化
     * @param {string} turnServerUrl turn server url
     * @param {string} turnServerCredential  turn server password
     * @param {string} turnServerUsername  turn server username
     */
    constructor(turnServerUrl, turnServerCredential, turnServerUsername) {
        if (turnServerUrl) {
            let ccOptions = {
                rtcConfiguration: {
                    iceServers: [{
                        urls: [
                            turnServerUrl
                        ],
                        credential: turnServerCredential,
                        username: turnServerUsername
                    }]
                }
            };
            this.irtcClient = new irtc.ConferenceClient(ccOptions);
        } else {
            this.irtcClient = new irtc.ConferenceClient();
        }
        this.participants = [];
    }

    /**
     * 加入房间
     * @param {string} roomToken 加入房间的鉴权token
     * @returns Promise.<object, Error>
     */
    joinRoom(roomToken) {
        return new Promise((resolve, reject) => {
            this.irtcClient.join(roomToken).then(function (resp) {
                resolve(resp);
            }).catch(function (error) {
                reject(error);
            });
        });
    }

    /**
     * 离开房间
     * @returns Promise.<void, Error>
     */
    leaveRoom() {
        return new Promise((resolve, reject) => {
            this.irtcClient.leave().then(function (resp) {
                Logger.info(`ivcs-js-sdk:leave room success',${resp},${new Date()}`);
                resolve(resp);
            }).catch(function (error) {
                Logger.info(`ivcs-js-sdk:leave room error',${error},${new Date()}`);
                reject(error);
            })
        });
    }
}

export {
    IRtcMiniClient
}